// pages/mypage/invoice/invoice.js
const request = require('@/utils/request.js')
const constants = require('@/utils/khyConstants.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '1',//1新增；2查询
    packageId: '',
    packageNo: '',
    invoiceTitle: '',
    invoiceNo: '',
    userEmail: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app = getApp();
    let data = getApp().globalData.pageData[constants.page_invoice]
    this.setData({
      my_handle3:app.globalData.nginxIp+'/img/my_handle3.png',
      type: data.type,
      packageId: data.keyId,
      packageNo: data.packageNo
    })
    if (data.type == '2') {
      this.getPageData()
    }
    
  },

  getPageData() {
    request.get('/UserController/getUserInvoiceByPackageId', {packageId: this.data.packageId}).then(res => {
      if (res.data.code == constants.resultSuccessCode
        && res.data.result) {
          let data = Object.assign({}, this.data, res.data.result || {})
          this.setData(data)
      }
    })
  },

  onInput(e) {
    let name = e.currentTarget.dataset.name
    let value = e.detail.value
    this.setData({
      [`${name}`]: value
    })
  },

  onCommit() {
    let user = getApp().getUserInfo()
    let data = Object.assign({}, this.data, {userNo: user.userNo})
    request.post('/UserController/saveInvoice', data).then(res => {
      if (res.data.code == constants.resultSuccessCode) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success: res => {
            setTimeout(function() {
              wx.navigateBack()
            }, 2000)
          }
        })
      } else {
        wx.showToast({
          title: res.data.message || '提交失败',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})